package java67_adv8;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.*;

import java67_adv6.GetNextBirthday;

public class Achaoshi implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// Scanner input = new Scanner(System.in);

	Map<Integer, Huiyuan> huiyuan = new HashMap<>();

	public static final String FNAME = "supermarket.txt";

	// 从化的文件中读取Achaoshi对象,没有则新建
	public static Achaoshi getStoreSuperMarket() {
		ObjectInputStream oin = null;
		try {
			oin = new ObjectInputStream(new FileInputStream(FNAME));
			return (Achaoshi) oin.readObject();
		} catch (Exception e) {
			System.out.println("对象不存在!");
			// e.printStackTrace();
		} finally {
			if (oin != null)
				try {
					oin.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

		}
		return new Achaoshi();
	}

	public void store() {
		try {
			ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream(FNAME));
			oout.writeObject(this);
			oout.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public boolean kaika(String a, String b) {// a=建立用户名b=密码
		// boolean kaika = true;
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MARCH);
		int Day = c.get(Calendar.DATE);
		// for (;;) {
		// System.out.println("请输入注册姓名：");
		// String a = input.next();
		// System.out.println("请输入注册密码：");
		// String b = input.next();
		if (b.length() >= 6) {
			int kahao = (int) (Math.random() * 90000000 + 10000000);
			System.out.println("恭喜开卡成功，系统送您100积分！您的会员卡号为：" + kahao);
			String riqi = String.format("%d-%d-%d", year, m + 1, Day);
			Huiyuan hy = new Huiyuan(a, b, 100, riqi);
			// huiyuan.add(hy);
			huiyuan.put(kahao, hy);
			return true;
		}
		System.out.println("会员密码不能少于6位，请重新输入:");
		return false;
	}

	// }

	public void gaimima(int a, String b) {
		// System.out.println("请输入您的会员卡号：");
		// System.out.println("请输入您的会员密码：");
		// int a = input.nextInt();
		// String b = input.next();
		Scanner input = new Scanner(System.in);
		if (huiyuan.containsKey(a) == true) {
			Huiyuan o = new Huiyuan();
			o = (Huiyuan) huiyuan.get(a);
			if (o.getMima().equals(b)) {
				System.out.println("请输入新的密码：");
				String mima1 = input.next();
				if (mima1.length() >= 6) {
					o.setMima(mima1);
				} else {
					System.out.println("会员密码不能少于6位，修改密码失败！");
				}
			} else {
				System.out.println("卡号未找到或者密码输入错误，修改密码失败！");
			}
		}
	}

	public void jifenleijia(int a, String b) {
		Scanner input = new Scanner(System.in);
		if (huiyuan.containsKey(a) == true) {
			Huiyuan o = new Huiyuan();
			o = (Huiyuan) huiyuan.get(a);
			if (o.getMima().equals(b)) {
				System.out.println("请输入购物金额：");
				// System.out.println(o.getJifen());
				o.setJifen(o.getJifen() + input.nextInt());
				// System.out.println(o.getJifen());
			} else {
				System.out.println("账号密码错误！");
			}
		}
	}

	public void jifenduihuan(int a, String b) {
		Scanner input = new Scanner(System.in);
		if (huiyuan.containsKey(a) == true) {
			Huiyuan o = new Huiyuan();
			o = (Huiyuan) huiyuan.get(a);
			if (o.getMima().equals(b)) {
				System.out.println("请输入需要兑换的积分：(只能兑换100的整数积分)");
				int jifen1 = input.nextInt();
				if (jifen1 % 100 == 0 && jifen1 < o.getJifen()) {
					System.out.println("您的消费金额中使用会员积分抵" + (double) (jifen1 / 1000) + "元");
					o.setJifen(o.getJifen() - jifen1);
					System.out.println("积分兑换成功！");
				} else {
					System.out.println("输入错误，或者积分不足,请重新选择。");
				}
			}
		}
	}

	public void chakanjifen(int a, String b) {
		if (huiyuan.containsKey(a) == true) {
			Huiyuan o = new Huiyuan();
			o = (Huiyuan) huiyuan.get(a);
			if (o.getMima().equals(b)) {
				System.out.println("您的积分为：" + o.getJifen());
			}
		}
	}

	public void chakan() {
		// Huiyuan hy = new Huiyuan();
		System.out.println(huiyuan);
	}

	public static void main(String[] args) {
		// Achaoshi cs = new Achaoshi();
		Achaoshi cs = getStoreSuperMarket();

		Scanner input = new Scanner(System.in);
		for (;;) {
			System.out.println("***********************欢迎进入超市会员管理系统***********************");
			System.out.println("1.开卡\t 2.修改密码\t 3.积分累计\t 4.积分兑换\t 5.查询剩余积分\t 6.查询所有客户 \t7.退出");
			System.out.println("**************************************************************");
			System.out.println("请选择：");
			int j = input.nextInt();
			switch (j) {
			case 1:
				for (;;) {
					System.out.println("请输入注册姓名：");
					String a = input.next();
					System.out.println("请输入注册密码：");
					String b = input.next();
					if (cs.kaika(a, b) == true) {
						break;
					}
				}
				break;
			case 2:
				System.out.println("请输入您的会员卡号：");
				int a1 = input.nextInt();
				System.out.println("请输入您的会员密码：");
				String b1 = input.next();
				cs.gaimima(a1, b1);
				break;
			case 3:
				System.out.println("请输入您的会员卡号：");
				int a2 = input.nextInt();
				System.out.println("请输入您的会员密码：");
				String b2 = input.next();
				cs.jifenleijia(a2, b2);
				break;
			case 4:
				System.out.println("请输入您的会员卡号：");
				int a3 = input.nextInt();
				System.out.println("请输入您的会员密码：");
				String b3 = input.next();
				cs.jifenduihuan(a3, b3);
				break;
			case 5:
				System.out.println("请输入您的会员卡号：");
				int a4 = input.nextInt();
				System.out.println("请输入您的会员密码：");
				String b4 = input.next();
				cs.chakanjifen(a4, b4);
				break;
			case 6:
				cs.chakan();
				break;
			case 7:
				break;
			default:
				System.out.println("输入错误，重新选择");
				break;
			}
			if (j == 7) {
				System.out.println("退出系统，谢谢使用！");
				cs.store();
				break;
			}
		}

	}
}
